package com.zt.algorithm.sort;

public class BubbleSort {
    public static void main(String args[]){
        int a[] = new int[]{5,7,1,8,4};
        BubbleSort sort = new BubbleSort();
        sort.BubbleSort(a);
        for (int m: a) {
            System.out.print(m + ",");
        }

    }
    public void BubbleSort(int a[]){
        boolean exchange; //如果在一次冒泡中没有出现交换次序，说明已经有序了
        int length = a.length;
        for (int i = 0; i<length -1 ;i++){ //外循环 n-1趟排序
            exchange = false;
            for (int j = 0 ;j<length-1-i; j++){ //内循环
                if(a[j] > a[j+1])
                {
                    int temp = a[j];
                    a[j]  = a[j+1];
                    a[j+1] = temp;
                    exchange = true;
                }
            }
            if(!exchange){
                return;
            }
        }
    }
}
